/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui.combination;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import lineage.ui.customize.L1Font;
import lineage.ui.util.L1LoadImage;
import lineage.util.Util;

/**
 * 单元格组件渲染器.
 * 
 * @version 2014年2月24日下午3:13:14
 * @author jrwz
 */
public final class L1ListCellRenderer extends JPanel implements ListCellRenderer<Object> {
    private static final long serialVersionUID = -1221957068560914021L;
    private String text;
    private Color background;
    private Color foreground;
    private Image img;

    protected L1ListCellRenderer() {
        setLayout(null);
        font();
        setOpaque(false); // 设置为透明
    }

    /**
     * 设置字体.
     */
    private void font() {
        L1Font.setFont(Font.BOLD, 14.0f);
        setFont(L1Font.getFont()); // 设置字体
    }

    @Override
    public Component getListCellRendererComponent(JList<? extends Object> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Object[] cell = (Object[]) value;
        text = cell[0].toString();
        setToolTipText(cell[1].toString()); // 提示信息
        image(cell[2].toString()); // 等待图像载入完成
        background = isSelected ? list.getSelectionBackground() : list.getBackground();
        foreground = isSelected ? list.getSelectionForeground() : list.getForeground();
        return this;
    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(background);
        g.fillRect(0, 0, getWidth(), getHeight()); // 设置背景色
        g.setColor(foreground);
        g.drawString(text, 23, 18); // 在指定位置绘制文本

        // super.paintComponent(g);
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(74, 27); // Cell的尺寸
    }

    /**
     * 图像处理.
     */
    private void image(String url) {
        img = L1LoadImage.getInstance().getImg(url);
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(img, 0);
        try {
            mt.waitForAll(); // 开始加载由此媒体跟踪器跟踪的所有图像。此方法将等待，直到所有被跟踪的图像已经完成加载。
        } catch (InterruptedException e) {
            Util.message(e.getLocalizedMessage());
        }
    }
}
